package com.app.h5.util;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.Window;

import com.app.h5.R;
import com.app.h5.databinding.LayoutCommonDialogBinding;

import androidx.annotation.NonNull;


public class CommonDialog extends Dialog {
    private LayoutCommonDialogBinding binding;

    @SuppressLint("InflateParams")
    public CommonDialog(@NonNull Context context, boolean cancel, OnCommitListener onConfirmListener) {
        super(context, R.style.dialog1);
        binding = LayoutCommonDialogBinding.inflate(LayoutInflater.from(context));
        setContentView(binding.getRoot());
        setCanceledOnTouchOutside(cancel);
        Window window = getWindow();//设置窗体位置以及动画
        if (null != window) {
            //window.setGravity(Gravity.BOTTOM);
            window.setWindowAnimations(android.R.style.Animation_Toast);
            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        binding.confirm.setOnClickListener(view -> {
            dismiss();
            if (onConfirmListener != null) {
                onConfirmListener.confirm();
            }
        });
        binding.close.setOnClickListener(view -> {
            dismiss();
            if (onConfirmListener != null) {
                onConfirmListener.close();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public interface OnCommitListener {
        void close();

        void confirm();
    }
}
